/**
 * Created by novem on 2015/11/19.
 */
var purl = getPicurl();
window.onload=function() {
    var url = getUrl();

    var photobookId = $.getUrlParam("photobookId");
    $.jsonp({
        url: 'http://' + url + '/api/photobook/edit?photoBookId='+photobookId,
        data: {},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            var strs=data.urlList.split(","); //字符分割
            for (var i=0;i<strs.length ;i++ )
            {
                var html = '<li class="imgwrapper" style="width: 74px;"><img style="width:60px;height:60px;" src="'+purl+strs[i]+'" alt="">' +
                    '<a class="close" href="javascript:;"></a></li>';
                $(".pic_list").append(html);

            }
            $("#photo_title").val(data.name);
            var spotsId = data.spotsId;
            $.jsonp({
                url: 'http://' + url + '/api/photobook/option_spotsphoto',
                data: {},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    var complied_tpl = juicer($("#fabu_tpl").text());
                    var html = complied_tpl.render(data);
                    $("#selected").append(html);
                    var photoName = $("#data-"+spotsId+"").text();
                    $(".sss").text(photoName);
                    $(".sss").attr("id",spotsId);
                },
                error: function (xOptions, textStatus) {
                }
            });

            $(".ui-btn-lg").on("click",function(){
                var urlList = getPics();
                var name=$("#photo_title").val();
                var userId = getUserId();
                var spotsId =$("#selected").find("option:checked").attr("id");
                if(!name){
                    showToast("画册名不能为空~");
                    return;
                }
                if(!spotsId){
                    showToast("请选择景点画册~");
                    return;
                }
                if(!urlList){
                    showToast("请至少上传一张图片~");
                    return;
                }
                $.jsonp({
                    url: 'http://'+url+'/api/photobook/update?photoBookId='+photobookId,
                    data: {name:name,userId:userId,spotsId:spotsId,urlList:urlList},
                    callbackParameter: "callback",
                    success: function (data, textStatus, xOptions) {
                        if(data.succ==true){
                            //取得用户ID
                            location.href="wode_huace_chuangjian.html";
                        }
                    },
                    error: function (xOptions,textStatus) {
                    }
                });
            });

        },
        error: function (xOptions, textStatus) {
        }
    });

}
function getPics(){
    var imgs=$("#ul img");
    //alert(imgs.length);
    var paths=[];
    $.each(imgs,function(index,ele){
        if(index != imgs.length - 1){
            var piccs = $(ele).attr('src').substr(purl.length);
            if(piccs){paths.push(piccs);}
        }
    })
    var paths=paths.join(",");
    return paths;
}

$(document).on('click','.close',function(){
    $(this).parent().remove();
});